package com.wind.stream;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Stream的使用：归集
 *
 * @author Wind
 * @date 2021-06-09
 */
public class Test07Stream {
    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 6, 3, 4, 4, 5, 7, 9, 6, 21);
        list.forEach(System.out::print);
        System.out.println();

        // toList
        List<Integer> newList = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
        newList.forEach(System.out::print);
        System.out.println();

        // toSet
        Set<Integer> newSet = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet());
        newSet.forEach(System.out::print);
        System.out.println();

        // toMap
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("张三", 7000, 34, "男", "陕西"));
        persons.add(new Person("李四", 8000, 24, "男", "河北"));
        persons.add(new Person("王五", 6000, 42, "女", "甘肃"));
        persons.add(new Person("王五", 9000, 12, "女", "上海"));
        Map<String, Person> map = persons.stream().filter(p -> p.getSalary() > 6000).collect(Collectors.toMap(Person::getName, p -> p));
        System.out.println(map);
    }
}
